<template>
	<el-container>
		<el-container>
			<el-header>
				<div class="left-panel">
					<div class="right-panel-search">
                        <el-input v-model="search.keyword" placeholder="请输入名称/联系人/手机号" clearable @change="handleKeyDown" />
						<el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
					</div>
				</div>
			</el-header>
            <el-header>
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'custom.add'">新增</el-button>
            </el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" stripe remoteSort remoteFilter border>
					<el-table-column label="客户全称" fixed prop="apply_name" align="center" width="130"></el-table-column>
					<el-table-column label="客户简称" fixed prop="referred_as" align="center" width="130"></el-table-column>
					<el-table-column label="联系人" fixed prop="linkman" align="center" width="100"></el-table-column>
					<el-table-column label="联系方式" prop="mobile" align="center" width="120"></el-table-column>
					<el-table-column label="联系地址" prop="address" align="center" width="130"></el-table-column>
					<el-table-column label="客户等级" prop="grade_text" align="center" width="80"></el-table-column>
					<el-table-column label="税号" prop="tax_identification_no" align="center" width="130"></el-table-column>
					<el-table-column label="客户交期时长" prop="custom_delivery_time" align="center" width="100"></el-table-column>
					<el-table-column label="生产交期时长" prop="produce_delivery_time" align="center" width="100"></el-table-column>
					<el-table-column label="出货允差" prop="sell_tolerance" align="center" width="100"></el-table-column>
					<el-table-column label="结算方式" prop="settlement_way_text" align="center" width="80"></el-table-column>
                    <el-table-column label="是否禁用" prop="system_disable" align="center" width="80">
                        <template #default="scope">
                            <el-tag v-if="scope.row.system_disable==1" type="success">是</el-tag>
						    <el-tag v-else type="danger">否</el-tag>
						</template>
                    </el-table-column>
					<el-table-column label="母单发货" prop="is_master_order_send" align="center" width="80">
                        <template #default="scope">
                            <el-tag v-if="scope.row.is_master_order_send==1" type="success">是</el-tag>
						    <el-tag v-else type="danger">否</el-tag>
						</template>
                    </el-table-column>
					<el-table-column label="出货要求" prop="sell_ask" align="center" width="110">
                        <template #default="scope">
                            <el-button text type="primary" size="small" @click="show_ask(scope.row, scope.$index)" v-auth="'custom.edit'">查看要求</el-button>
                            <!-- <el-popover
                                placement="top-start"
                                :width="200"
                                trigger="hover"
                                :content="scope.row.sell_ask"
                            >
                            <template #reference>
                                <el-button style="padding: 5px 0;" text class="m-2">{{scope.row.sell_ask.substring(0, 15)}}</el-button>
                            </template>
                            </el-popover> -->
						</template>
                    </el-table-column>
					<el-table-column label="品牌信息" prop="brand_arr" align="center" width="130">
                        <template #default="scope">
							<div v-for="item in scope.row.brand_arr" :key="item.id">{{item.brand_name}}</div>
						</template>
                    </el-table-column>
					<el-table-column label="操作" fixed="right" align="center" width="75">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'custom.edit'">修改</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="danger" size="small" v-auth="'custom.delete'">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
    <ask v-if="dialog.ask" ref="askDialog" @success="handleSuccess" @closed="dialog.ask = false" draggable></ask>
</template>

<script>
import Ask from './ask.vue'
import saveDialog from './save'

export default {
	name: 'user',
	components: {
		saveDialog,Ask
	},
	data() {
		return {
			dialog: {
				save: false,
                ask:false,
			},
			apiObj: this.$API.customer.getCustomerList.lists,
			selection: [],
			search: {
				keyword: null,
				cid: null,
			},
			//所需数据选项
			customer: [],
		}
	},
	mounted() {
        this.getCustomer()
	},
	methods: {
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//删除
		async table_del(row, index) {
            console.log('row :>> ', row);
			var reqData = { custom_id: row.custom_id }
			var res = await this.$API.customer.getCustomerList.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
        show_ask(row){
            this.dialog.ask = true
			this.$nextTick(() => {
				this.$refs.askDialog.open('edit').setData(row)
			})
        },
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'set') {
				this.$refs.table.refresh()
			}
		},
        // 获取所有客户
        async getCustomer(){
            var res = await this.$API.all.customer.get();
            this.customer = res.data;
        },
        handleKeyDown() {
            this.upsearch()
        },
	}
}
</script>

<style>
.search-input {
	width: auto;
	font-size: 14px;
}
</style>
